package com.carassistant.beta.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.carassistant.beta.R;
import com.carassistant.beta.common.CarAssistantApp;
 

public class MAlertDialog extends Dialog{
	public static final int DIALOG_WRAP_CONTENT = 0;
	public static final int DIALOG_TEXT = 1;
	public static final int DIALOG_LIST = 2;
	
	private TextView titleText;
	private TextView messageText;
	private ListView list;
	private Context context;  
	
	private int whichItem;
	
	private ListAdapter adapter;
	private String title;
	private String message;
	private ScrollView scrollMsg;
	
	private Button ok;
	private Button netrue;
	private Button cancel;
	
	private OnItemClickListener itemListener;
	private View.OnClickListener okListener;
	private View.OnClickListener netrueListener;
	private View.OnClickListener cancelListener;
	
	private String okText;
	private String netrueText;
	private String cancelText;  
	
	private LinearLayout checkboxLayout;
	private CheckBox checkBox;
	private OnCheckedChangeListener checkedListener;
	private int dialogType = 0;
	private int msgLines = 3;
	public MAlertDialog(Context context, int dialogType) {
		super(context, R.style.new_dialog); 
		cancelText = context.getString(R.string.cancel);
		this.context = context;
		this.dialogType = dialogType;
	}
	 

	public MAlertDialog(Context context, int theme,  int dialogType) {
		super(context, theme); 
		this.context = context;
		this.dialogType = dialogType;
	}
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState); 
		requestWindowFeature(Window.FEATURE_NO_TITLE);   
		setContentView(R.layout.m_alert_dialog);  
		
		if (title != null) {
			titleText = (TextView) findViewById(R.id.title);
			titleText.setVisibility(View.VISIBLE);  
			titleText.setText(title);
		}
		
		if (message != null) {
			messageText= (TextView) findViewById(R.id.message);
			scrollMsg = (ScrollView) findViewById(R.id.scroll_message);
			scrollMsg.setVisibility(View.VISIBLE);
			messageText.setText(message);
		}
		
		if (checkedListener != null) {
			checkboxLayout = (LinearLayout) findViewById(R.id.checkbox_layout);
			checkboxLayout.setVisibility(View.VISIBLE);
			checkBox = (CheckBox) findViewById(R.id.checkbox);
			checkBox.setOnCheckedChangeListener(checkedListener); 
		}
		
		if (itemListener != null) {
			list = (ListView) findViewById(R.id.list);
			list.setAdapter(adapter); 
			list.setOnItemClickListener(itemListener); 
			list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			list.setVisibility(View.VISIBLE);  
			list.setItemChecked(whichItem, true);
		}
		     
		if (okListener != null) {
			ok = (Button) findViewById(R.id.ok);
			ok.setOnClickListener(okListener);
			ok.setVisibility(View.VISIBLE);
			ok.setText(okText);
		}
		
		
		if (netrueListener != null) {
			netrue = (Button) findViewById(R.id.neture);
			netrue.setVisibility(View.VISIBLE); 
			netrue.setText(netrueText);
			netrue.setOnClickListener(netrueListener);
		} 
		
		cancel = (Button) findViewById(R.id.cancel);
		cancel.setVisibility(View.VISIBLE); 
		cancel.setText(cancelText);
		if (cancelListener != null) {
			cancel.setOnClickListener(cancelListener);
		} else {
			cancel.setOnClickListener(defaultListener);
		}
		
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes(); 
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = CarAssistantApp.screenMin * 9 / 10; // 宽度     
		if(dialogType == DIALOG_WRAP_CONTENT){
			lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度
		} else if(dialogType == DIALOG_TEXT){
			lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度
			messageText.setLines(msgLines); 
		} else if(dialogType == DIALOG_LIST){
			lp.height = CarAssistantApp.screenMin * 9 / 10; // 高度
		}
		 
		dialogWindow.setAttributes(lp); 
	}
	    
	public void setListItemChecked(int position){
		if (list != null) {
			list.setItemChecked(position, true);
		}
	}
	
	public void setTitle(CharSequence title) {
		if (title != null) {
			this.title = title.toString();
		} 
	}
	
	public void setLines(int lines){
		msgLines = lines;
	}
	
	public void setTitle(int titleId) { 
		this.title = context.getString(titleId); 
	}  
	
	public void setMessage(CharSequence message) {
		if (message != null) {
			this.message = message.toString();
		} 
	}
	
	public void setMessage(int messageId) { 
		this.message = context.getString(messageId); 
	} 
	
	public void setAdapter(ListAdapter adapter, OnItemClickListener listener) {
		this.adapter = adapter;
		this.itemListener = listener; 
	} 
	
	public void setPositiveButton(String text, View.OnClickListener l){
		this.okText = text;
		this.okListener = l;
	}
	
	public void setPositiveButton(int textId, View.OnClickListener l){
		this.okText = context.getString(textId);
		this.okListener = l;
	}
	
	public void setNetureButton(String text, View.OnClickListener l){
		this.netrueText = text;
		this.netrueListener = l;
	}
	
	public void setNetureButton(int textId, View.OnClickListener l){
		this.netrueText = context.getString(textId);
		this.netrueListener = l;
	}
	
	public void setNegativeButton(String text, View.OnClickListener l){
		this.cancelText = text;
		this.cancelListener = l;
	}
	
	public void setNegativeButton(int textId, View.OnClickListener l){
		this.cancelText = context.getString(textId);
		this.cancelListener = l;
	}
	
	public void setSingleChoiceItems(int textID, int whichItem, boolean font,OnItemClickListener l){
		this.itemListener = l;
		CharSequence[] temps =  context.getResources().getTextArray(textID); 
		this.whichItem =whichItem;
		adapter = new SimgleChoiceAdapter(context, temps,font);
	}
	
	public void setSingleChoiceItems(String[] texts, int whichItem, OnItemClickListener l){
		this.itemListener = l; 
		this.whichItem =whichItem;
		adapter = new SimgleChoiceAdapter(context, texts,false);
	} 
	 
	public void setCheckboxListener(OnCheckedChangeListener l){
		this.checkedListener = l;
	} 
	
	private View.OnClickListener defaultListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) { 
			dismiss();
		}
	}; 
	
}
